package it.unina.multimodal;

import it.unina.lab.managers.Controller;
import it.unina.lab.modality.ModalityComponent;
import it.unina.lab.model.ModalityEvent;
import it.unina.lab.multimodal.R;

import java.util.ArrayList;

import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.widget.ImageButton;


public class SpeechModality extends ModalityComponent implements RecognitionListener {

	ImageButton mic = null;
	
	public SpeechModality(String modality, Controller controller, ImageButton button) {
		super(modality, controller);
		mic = button;
		// TODO Auto-generated constructor stub
	}
	
	
	
	public void onReadyForSpeech(Bundle params)
    {
			 mic.setImageResource(R.drawable.micblu);
             Log.d("Multimodal", "onReadyForSpeech");
    }
    
	public void onBeginningOfSpeech()
    {
             Log.d("Multimodal", "onBeginningOfSpeech");
    }
    
    public void onRmsChanged(float rmsdB)
    {
             Log.d("Multimodal", "onRmsChanged - value: " + rmsdB);
    }
    
    public void onBufferReceived(byte[] buffer)
    {
             Log.d("Multimodal", "onBufferReceived");
    }
    
    public void onEndOfSpeech()
    {	
    		 mic.setImageResource(R.drawable.micgrigio);
             Log.d("Multimodal", "onEndofSpeech");
    }
    
    public void onError(int error)
    {
             Log.d("Multimodal",  "error " +  error);
        
    }
    
    public void onResults(Bundle results){
             Log.d("Multimodal", "onResults " + results);
             ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            /* for (int i = 0; i < data.size(); i++)
             {
                       Log.d("Multimodal", "result " + data.get(i));
                       sendEvent(new ModalityEvent("speech", "task", (String)data.get(i)));
             } */
             String frase = (String) data.get(0);
             Log.d("Multimodal", "result " + frase);
             if(frase.contains("rosso"))
            	 sendEvent(new ModalityEvent("speech", "task", "rosso"));
             else if(frase.contains("bianco"))
            	 sendEvent(new ModalityEvent("speech", "task", "bianco"));
                
    }



	@Override
	public void onEvent(int eventType, Bundle params) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void onPartialResults(Bundle partialResults) {
		// TODO Auto-generated method stub
		
	}
    
	
	

	
}
